$$fx = window.getComputedStyle ? true : false; // to revise

if ( (typeof ria) == 'undefined')
	ria = rialto;
if ((typeof ria.utils) == 'undefined')
	ria.utils = {};
	
ria.utils.measures = {};

// <oHtml>.offsetHeight :
//		Fx count borders
//		IE count borders
// <oHtml>.style.height :
//		Fx dont count border
//		IE count border

ria.utils.measures.$getHeight = function (oHtml,bBorderLess,bNotAlreadyInDoct)  {
//==========================
// return height of object Html, in pixels, 
// by counting borders (or not);
// the Html object is already insert (or not) in document.

// oHtml                     i (Html Elt) : considered Html Object 
// [bBorderLess       false] i (boolean)  : flag / borders counting
// [bNotAlreadyInDoct false] i (boolean)  : flag / not registered in document

if (bNotAlreadyInDoct) // non available in IE
	alert('not yet implemented');
	
else {
	// to revise : test insertion in doct with getElementById or other method
	return ria.utils.measures.$$getHeightInDoct(oHtml,bBorderLess);
}

}
ria.utils.measures.$$getHeightInDoct = function (oHtml,bBorderLess) {
//==================================
// oHtml must be in document or style.height affected with a value
	var wH;
	if (oHtml.nodeName=="BODY")
        wH = document.body.clientHeight;
    else 
    	// wH =  oHtml.offsetHeight; mod 13/06/06 : if oHtml not yet in document
		/*wH = (oHtml.offsetHeight ? oHtml.offsetHeight 
								 : parseInt(oHtml.style.height));
		*/
		wH = (oHtml.offsetHeight || parseInt(oHtml.style.height));

	var b = 0;
	if (bBorderLess) {
		b -= ria.utils.measures.$$getBorderTopWidth(oHtml); 
		b -= ria.utils.measures.$$getBorderBottomWidth(oHtml); 
	}	

	return (wH + b);
}

ria.utils.measures.$$getBorderTopWidth = function(oHtml) {
//====================================
if (!oHtml) return 0;
if (   (oHtml.nodeName == 'BODY')
	|| (oHtml.nodeName == '#document'))
	 return 0;
var topEdge = 0;
if (!window.getComputedStyle) {
	if	(	 ( 	  (oHtml.style.borderTopStyle != 'none')		
		 	  &&  (oHtml.style.borderTopStyle != ''))
		 ||	 (	  oHtml.currentStyle
			  &&  (oHtml.currentStyle.borderTopStyle != 'none')		
			  &&  (oHtml.currentStyle.borderTopStyle != ''))
		) 
		topEdge = 	parseInt(oHtml.style.borderTopWidth)
				 || parseInt(oHtml.currentStyle.borderTopWidth);
}
else {
	if (	(window.getComputedStyle(oHtml,null).getPropertyValue('border-top-style') != 'none')		
		&&  (window.getComputedStyle(oHtml,null).getPropertyValue('border-top-style') != '')) {
		if (window.getComputedStyle(oHtml,null).getPropertyValue('border-top-width')) 
			topEdge = parseInt(window.getComputedStyle(oHtml,null).getPropertyValue('border-top-width'));
		
	}
}
return topEdge;
}

ria.utils.measures.$$getBorderBottomWidth = function(oHtml) {
//====================================
//if (oHtml.nodeName == 'BODY') return 0;
if (!oHtml) return 0;
if (   (oHtml.nodeName == 'BODY')
	|| (oHtml.nodeName == '#document'))
	 return 0;
var bottomEdge = 0;
if (!window.getComputedStyle) {
	if	(	 ( 	  (oHtml.style.borderBottomStyle != 'none')		
		 	  &&  (oHtml.style.borderBottomStyle != ''))
		 ||	 (	  oHtml.currentStyle
			  &&  (oHtml.currentStyle.borderBottomStyle != 'none')		
			  &&  (oHtml.currentStyle.borderBottomStyle != ''))
		) 
		bottomEdge = 	parseInt(oHtml.style.borderBottomWidth)
				 || parseInt(oHtml.currentStyle.borderBottomWidth);
}
else {
	if (	(window.getComputedStyle(oHtml,null).getPropertyValue('border-bottom-style') != 'none')		
		&&  (window.getComputedStyle(oHtml,null).getPropertyValue('border-bottom-style') != '')) {
		if (window.getComputedStyle(oHtml,null).getPropertyValue('border-bottom-width')) 
			bottomEdge = parseInt(window.getComputedStyle(oHtml,null).getPropertyValue('border-bottom-width'));
		
	}
}
return bottomEdge;
}

ria.utils.measures.$$getBorderRightWidth = function(oHtml) {
//====================================
//if (oHtml.nodeName == 'BODY') return 0;
if (!oHtml) return 0;
if (   (oHtml.nodeName == 'BODY')
	|| (oHtml.nodeName == '#document'))
	 return 0;
var rightEdge = 0;
if (!window.getComputedStyle) {
	if	(	 ( 	  (oHtml.style.borderRightStyle != 'none')		
		 	  &&  (oHtml.style.borderRightStyle != ''))
		 ||	 (	  oHtml.currentStyle
			  &&  (oHtml.currentStyle.borderRightStyle != 'none')		
			  &&  (oHtml.currentStyle.borderRightStyle != ''))
		) 
		rightEdge = 	parseInt(oHtml.style.borderRightWidth)
				 || parseInt(oHtml.currentStyle.borderRightWidth);
}
else {
	if (	(window.getComputedStyle(oHtml,null).getPropertyValue('border-right-style') != 'none')		
		&&  (window.getComputedStyle(oHtml,null).getPropertyValue('border-right-style') != '')) {
		if (window.getComputedStyle(oHtml,null).getPropertyValue('border-right-width')) 
			rightEdge = parseInt(window.getComputedStyle(oHtml,null).getPropertyValue('border-right-width'));
		
	}
}
return rightEdge;
}

ria.utils.measures.$$getBorderLeftWidth = function(oHtml) {
//====================================
 //traceExec('oHtml.nodeName= '+oHtml.nodeName,73);
if (!oHtml) return 0;
if (   (oHtml.nodeName == 'BODY')
	|| (oHtml.nodeName == '#document'))
	 return 0;
var leftEdge = 0;
if (!window.getComputedStyle) {
	if	(	 ( 	  (oHtml.style.borderLeftStyle != 'none')		
		 	  &&  (oHtml.style.borderLeftStyle != ''))
		 ||	 (	  oHtml.currentStyle
			  &&  (oHtml.currentStyle.borderLeftStyle != 'none')		
			  &&  (oHtml.currentStyle.borderLeftStyle != ''))
		) 
		leftEdge = 	parseInt(oHtml.style.borderLeftWidth)
				 || parseInt(oHtml.currentStyle.borderLeftWidth);
}
else {
	if (	(window.getComputedStyle(oHtml,null).getPropertyValue('border-left-style') != 'none')		
		&&  (window.getComputedStyle(oHtml,null).getPropertyValue('border-left-style') != '')) {
		if (window.getComputedStyle(oHtml,null).getPropertyValue('border-left-width')) 
			leftEdge = parseInt(window.getComputedStyle(oHtml,null).getPropertyValue('border-left-width'));
		
	}
}
return leftEdge;
}

ria.utils.measures.$setHeightConformW3C = function (oHtml, oHtmlRef) {
//=====================================
// for IE :  Calculate  the external height supplied by the current value of the attribute
// style.height conform to W3C norme (box model size as in FireFox )
// for IE :  computed  the size conform to W3C norme (as FireFox )
//(ex:  if oHtml.Style.height == 40px and border top+bottom widths == 5px + 5px 
//--> new height = 40 + 5 + 5 = 50px)

// if oHtmlRef :
// the oHtml extern height (with borders) must be the same as oHtmlRef extern heigth
// for IE and Fx

//usage restriction : if style is declared in css, oHtml must be already loaded in document (to be able use currentStyle attribute)

if (oHtmlRef) {
	// model W3C (for ex : Fx)
	if (window.getComputedStyle) {
		oHtml.style.height =   _rum.$getHeight(oHtmlRef);
							-  _rum.$$getBorderTopWidth(oHtml)
							-  _rum.$$getBorderBottomWidth(oHtml);
							
	}
	// model IE
	else {
		oHtml.style.height = _rum.$getHeight(oHtmlRef);

	}
}

if (!window.getComputedStyle) {
	oHtml.style.height =  _rum.$getHeight(oHtml)
						//parseInt(oHtml.style.height) //modif 12/05 to try
						+ ria.utils.measures.$$getBorderTopWidth(oHtml)
						+ ria.utils.measures.$$getBorderBottomWidth(oHtml);	
}	
return (parseInt(oHtml.style.height));	
}

ria.utils.measures.$setWidthConformW3C = function (oHtml, oHtmlRef) {
//=====================================
// i.e. oHtml.style.width = oHtmlRef.offsetHidth
// oHtmlRef is optional : current oHtml's width is used if oHtmlRef is not 
// for IE :  Calculate  the eternal width supplied by the current value of the attribute
// style.width conform to W3C norme (box model size as in FireFox )
//(ex:  if oHtml.style.width == 40px and border left+right widths == 5px + 5px 
//--> new oHtml.style.width in IE = 40 + 5 + 5 = 50px)
//
// if oHtmlRef :
// the oHtml extern width (with borders) must be the same as oHtmlRef extern width
// for IE and Fx

//usage restriction : if style is declared in css (not in-line, js), oHtml must be already loaded in document 
//(to be able to use currentStyle attribute)

if (oHtmlRef) {
	// model W3C (for ex : Fx)
	if (window.getComputedStyle) {
		oHtml.style.width =    _rum.$getWidth(oHtmlRef)
							-  _rum.$$getBorderLeftWidth(oHtml)
							-  _rum.$$getBorderRightWidth(oHtml);
	}
	// model IE
	else {
		oHtml.style.width = _rum.$getWidth(oHtmlRef);
	}
}

if (!window.getComputedStyle) {
	oHtml.style.width = _rum.$getWidth(oHtml)
						//parseInt(oHtml.style.width) //modif 12/05 to try
						+ ria.utils.measures.$$getBorderLeftWidth(oHtml)
						+ ria.utils.measures.$$getBorderRightWidth(oHtml);	
}	
return (parseInt(oHtml.style.width));	
}

ria.utils.measures.$setSizeConformW3C = function (oHtml, oHtmlRef) {
//=====================================
//traceExec('$setSizeConformW3C ',2);
ria.utils.measures.$setHeightConformW3C (oHtml, oHtmlRef);
ria.utils.measures.$setWidthConformW3C (oHtml, oHtmlRef);
}


ria.utils.measures.$getWidth = function (oHtml,bBorderLess,bNotAlreadyInDoct)  {
//==========================
// return width of object Html, in pixels, 
// by counting borders (default)(or not);
// the Html object is already insert (or not) in document.

// oHtml                     i (Html Elt) : considered Html Object 
// [bBorderLess       false] i (boolean)  : flag / borders counting
// [bNotAlreadyInDoct false] i (boolean)  : flag / not registered in document

if (bNotAlreadyInDoct)
	alert('not yet implemented, impossible with IE ?');
	
else {
	// to revise : test insertion in doct with getElementById or other method
	return ria.utils.measures.$$getWidthInDoct(oHtml,bBorderLess);
}
}
ria.utils.measures.$$getWidthInDoct = function (oHtml,bBorderLess) {
//==================================
// oHtml must be in document or style.width affected with a value
	var wW;
	if (oHtml.nodeName=="BODY")
        wW = document.body.clientWidth;
    else
//		wW = oHtml.offsetWidth; mod 13/06/06 : if oHtml not yet in document
		wW = (oHtml.offsetWidth || parseInt(oHtml.style.width));
		
	var b = 0;
	if (bBorderLess) {
		b -= ria.utils.measures.$$getBorderLeftWidth(oHtml); 
		b -= ria.utils.measures.$$getBorderRightWidth(oHtml); 
	}	

	return (wW + b);
}

ria.utils.measures.$getStyle = function (oHtml,style) {
//==========================
var s;
if (window.getComputedStyle) 
	s = window.getComputedStyle(oHtml,null)[style];
else
	s =    oHtml.style[style] 
		|| oHtml.currentStyle[style];
/*
if (s == 'auto') {
	switch (style) {
		case 'top' : 
}...
*/
return s;
}



ria.utils.measures.$centerW=function (oHtml){
//==============================
//oHtml will be center according to the width of his parent
   Pw=oHtml.parentNode.offsetWidth/2;
   Ow=oHtml.offsetWidth/2;
   oHtml.style.left=(Pw-Ow)+"px";
}




ria.utils.measures.$divInternalTopLeftInViewPort =
ria.utils.measures.$internalPosInViewPort = function (oHtml)  {
//========================================
	var topLeftExtern = ria.utils.measures.$posInViewPort(oHtml);
	return { top  : (topLeftExtern.top  + _rum.$$getBorderTopWidth(oHtml)),
			 left : (topLeftExtern.left + _rum.$$getBorderLeftWidth(oHtml))
			};
}

ria.utils.measures.$divTopLeftInViewPort =
ria.utils.measures.$posInViewPort = function (oHtml)  {
//===============================
	var x = y = 0;
	
	while (		oHtml 
			&& (oHtml.nodeName != 'BODY')
			&& (oHtml.nodeName != '#document')
			   ) {
		x =    x 
			 + oHtml.offsetLeft
			 + ria.utils.measures.$$getBorderLeftWidth(oHtml.parentNode)
			 - oHtml.scrollLeft ;
		y =    y
			 + oHtml.offsetTop
			 + ria.utils.measures.$$getBorderTopWidth(oHtml.parentNode) 
			 - oHtml.scrollTop ;
//		oHtml = oHtml.parentNode; 
		oHtml = oHtml.offsetParent; 
		
		//traceExec('tag= '+oHtml.tagName+'oHtml.id= '+oHtml.id+' x= '+x+' y= '+y,73);
	}
	
	return { left: x
			,top: y}; 
}

ria.utils.measures.$divTopLeftInDiv = function (oHtml){
//=================================
	var x = y = 0;
	x =    oHtml.offsetLeft
		 + ria.utils.measures.$$getBorderLeftWidth(oHtml.parentNode) ;
	y =    oHtml.offsetTop
		 + ria.utils.measures.$$getBorderTopWidth(oHtml.parentNode) ;
	
	return { left: x
			,top: y}; 
}

ria.utils.measures.$eventTopLeftInViewPort = function(e) {   //, topLeftEvent) {
//========================================
if (!e) var e = window.event;
/* other option , to revise (test with scroll)

		var oHtmlTarget = (e.target ? e.target: e.srcElement);
		var topLeftDiv = ria.utils.measures.$divTopLeftInViewPort(oHtmlTarget);
		var topLeftEventInDiv = ria.utils.measures.$eventTopLeftInDiv(e);
		
		return {top  : topLeftDiv.top  + topLeftEventInDiv.top,
				left : topLeftDiv.left + topLeftEventInDiv.left
				}
*/

return {top : e.clientY
	    ,left : e.clientX
	    }
}

ria.utils.measures.$eventTopLeftInDiv =
ria.utils.measures.$offsetPosInDiv = function (e) {   //, oHtml) {
//==================================
    var oHtmlSource =    // oHtml ||
    				   (e.target ? e.target: e.srcElement);
//    traceExec('$eventTopLeftInDiv oHtmlSource.id = '+oHtmlSource.id,2);
    
	var left;
	if (e.offsetX != undefined) {
		left = 	e.offsetX
				+	ria.utils.measures.$$getBorderLeftWidth(oHtmlSource);
	}
	else {
		left = 	e.layerX;
	}
	
	var top;
	if (e.offsetY !=undefined) {

		top = 	e.offsetY
				+	ria.utils.measures.$$getBorderTopWidth(oHtmlSource);
	}
	else {
		top = 	e.layerY;
	}
	
	
	return {top: top, left:left};
}

ria.utils.$placeInViewPort = function (oHtml,basePointSpecs,shift) {
//========================
// oHtml     : oHtml element to place in the viewPort 
// basePoint : event objet or top-left objet
// shift     : shift / basePoint

// return -1 if enought height space available
//        the height space avalaible else

// 3, 9 ..etc to revise

	var top  = (basePointSpecs.top  != undefined)? basePointSpecs.top  : basePointSpecs.clientY;
	var left = (basePointSpecs.left != undefined)? basePointSpecs.left : basePointSpecs.clientX;

	var heightAvailable = -1;
	
    var rightSpace = document.body.clientWidth - left;
    var bottomSpace = document.body.clientHeight -top;
     
    //-- right space  
    if ( rightSpace < oHtml.offsetWidth ) {    
        oHtml.style.left = document.body.scrollLeft + left - oHtml.offsetWidth;
    }
    else {
        oHtml.style.left = document.body.scrollLeft + left;
    }    
    //--  bottom Space
    if (  oHtml.offsetHeight > bottomSpace  ) {
	    if (  oHtml.offsetHeight < (top - 3 - (shift || 0)) ) {
	        oHtml.style.top = document.body.scrollTop 
	        					+ top - oHtml.offsetHeight
	        					- 3
	        					- (shift || 0);			
	    }
	    else {
	    	if (top > bottomSpace) {
		    	heightAvailable   = top - 9 - (shift || 0);
	    	    oHtml.style.top = 3;			
	 	    	}
	    	else {
		    	heightAvailable   = bottomSpace - 3;
		        oHtml.style.top = document.body.scrollTop + top;
	    	}
//	    	window.status = 'not enought place to display the list correctly '; 
	    }
	}
    else {
        oHtml.style.top = document.body.scrollTop + top ;
    }
    
    
    return heightAvailable; 
}

_r = ria;
_ru = ria.utils;
_rum = ria.utils.measures;

